if (!GGServer.Class.webUI) GGServer.Class.webUI = {};
GGServer.Class.webUI.uploadFile = function(clientID, objname, divname, returninput, cplist, count) {
    count = 4;
    var o = this;
    o.Objname = objname;
    o.divName = divname;
    o.returnInput = returninput;
    o.UploadNum = (cplist.split('|').length > 4 ? 4 : cplist.split('|').length); //cplist.split('|').length;
    o.ClientPicList = cplist;
    o.IsShow = true;
    o.IsCheckType = true;
    o.ajaxSubmit = true;
    o.clientID = clientID;
    o.allowcount = count;
    o.PicListTemp;
    o.timerID0, o.timerID1, o.timerID2, o.timerID3;
    o.uplodEnableStr = ""; //上传图片的有效控件字符串,取消上传时将会清空对应的ID且不显示图片

    o.init = function() {
        //加载表单
        var divobj = document.getElementById(o.divName);
        var html = "";
        for (var i = 0; i < o.UploadNum; i++) {
            if (i >= o.allowcount) return;
            var style = o.style2.T;
            var datenow = new Date();
            var timeid = "t" + datenow.getMinutes() + datenow.getSeconds() + datenow.getMilliseconds();
            style = style.replace(/{TimeID}/g, timeid);
            style = style.replace(/{Num}/g, i);
            style = style.replace("{onchange}", o.Objname + ".Input_OnChange(this, " + i + ")");
            style = style.replace("{imgpath}", o.ClientPicList.split('|')[i]);
            html += style;
            //divobj.innerHTML +=style;		 
        }
        html += o.Hid.h;
        divobj.innerHTML = html;
        //加载数据
        if (typeof (o.ClientPicList) != "undefined") {
            document.getElementById(o.divName).style.display = '';
            for (var i = 0; i < o.ClientPicList.split("|").length && i < 4; i++) {
                if (i == o.allowcount) break;
                if (o.ClientPicList.split("|")[i] != '/img/nopic2.gif') {
                    document.getElementById("GGS_Upload_A" + i).style.display = "";
                    document.getElementById("GGS_UploadInput_" + i).style.display = "none";
                    document.getElementById("GGS_spanCover_" + i).style.display = "";
                }
                document.getElementById("GGS_UploadImg_" + i).src = o.ClientPicList.split("|")[i].replace('big', 'small');
                document.getElementById("PicList_" + i).value = o.ClientPicList.split("|")[i];
                //document.getElementById("GGS_UploadInput_"+i).disabled = true;
            }
            //document.getElementById("returnInput").value = ClientPicList;
        }
    }
    o.Input_OnChange = function(obj, num) {
        //给图片加载预览
        if (document.getElementById("GGS_UploadInput_" + num).value == "") {
            return;
        }
        clearErrTip(document.getElementById("Pic_message"));
        document.getElementById("GGS_UploadImg_" + num).src = "http://post.58.com/img/loadUpPic.gif";
        document.getElementById("GGS_UploadMsg_" + num).innerHTML = "正在上传。。。";
        document.getElementById("backNum").value = num;
        o.uplodEnableStr += num.toString();
        document.getElementById("GGS_UploadCancel_" + num).style.display = "";
        var f = document.getElementById("aspnetForm");
        f.action = "/upload";
        f.target = "formSubmit_" + num;
        f.submit();
        document.getElementById("GGS_UploadInput_" + num).disabled = true;
        document.getElementById("GGS_UploadProgress_" + num).innerHTML = '<div style="border:solid 1px #ccc; display:block; float:left; width:104px;"><div style="background-color:#99ccff; text-align:center; width:0%;">&nbsp;0%</div></div>';
        eval("o.timerID" + num + " = window.setInterval(\"initProgress(" + num + ")\", 1500)");
    }

    o.PicUploadFinish = function(type, url, num) {
        o.Cleartimer(num);
        if (type == "Yes") {
            if (o.uplodEnableStr.indexOf(num) == -1) return;
            setTimeout(function(){
            document.getElementById("GGS_UploadImg_" + num).src = "http://pic.58.com" + url.replace('big','small');
            },1000);
            document.getElementById("GGS_UploadMsg_" + num).innerHTML = "图片上传成功";
            document.getElementById("GGS_UploadCancel_" + num).style.display = "none";
            document.getElementById("Pic").value = document.getElementById("Pic").value + "|" + url;
            document.getElementById("PicList_" + num).value = url;
            o.fixPicString();
            document.getElementById("GGS_Upload_A" + num).style.display = "";
            o.setProgressFinish(num);
            document.getElementById("GGS_UploadInput_" + num).style.display = "none";
            o.uplodEnableStr.replace(num, "");
            document.getElementById("GGS_spanCover_" + num).style.display = "";
        } else {
            o.reSetUpload(num);
            if (type == "nopic") {
                type = "图片类型不正确";
            }
            else if (type == "notsize") {
                type = "图片大小不正确";
            }
            document.getElementById("GGS_UploadMsg_" + num).innerHTML = type;
        }
    }

    o.fixPicString = function() {
        var tmp = document.getElementById("Pic").value.toLowerCase();
        tmp = tmp.replace(/http:\/\/pic\.58\.com/g, "");
        tmp = tmp.replace(/http:\/\/post\.58\.com/g, "");
        tmp = tmp.replace(/\/img\/nopic2\.gif/g, "");
        tmp = tmp.replace(/(^\|*)|(\|*$)/g, "");
        tmp = tmp.replace(/\|\|/g, "|");
        var arr = tmp.split('|');
        if (arr.length > o.allowcount) {
            tmp = "";
            for (var i = 0; i < o.allowcount; i++) {
                tmp += arr[i] + "|";
            }
        }
        document.getElementById("Pic").value = tmp.replace(/(^\|*)|(\|*$)/g, "");
    }

    o.SetCover = function(num) {
        var tmp = document.getElementById("PicList_" + num).value;
        for (var i = 0; i < o.allowcount; i++) {
            if (document.getElementById("PicList_" + i).value.toLowerCase().indexOf("nopic") == -1 && i != num) {
                tmp += "|" + document.getElementById("PicList_" + i).value;
            }
        }
        document.getElementById("Pic").value = tmp;
        o.fixPicString();
    }
    o.setProgress = function(num, p) {
        if (o.uplodEnableStr.indexOf(num) == -1) return;
        document.getElementById("GGS_UploadProgress_" + num).innerHTML = '<div style="border:solid 1px #ccc; display:block;width:104px; float:left;"><div style="background-color:#99ccff; text-align:center; width:' + p + '%;">&nbsp;' + p + '%</div></div>';
    }
    o.setProgressFinish = function(num) {
        if (o.uplodEnableStr.indexOf(num) == -1) return;
        document.getElementById("GGS_UploadProgress_" + num).innerHTML = '<div style="border:solid 1px #ccc; display:block;width:104px;"><div style="background-color:#99ccff; text-align:center; width:100%;">&nbsp;100%</div></div>';
    }
    o.DelPic = function(num) {
        if (document.getElementById("PicList_" + num).value == "") {
            document.getElementById("GGS_UploadMsg_" + num).innerHTML = "请先上传图片。";
        } else {
            var pic = document.getElementById("PicList_" + num).value.toLowerCase().replace("http://pic.58.com", "");
            document.getElementById("Pic").value = document.getElementById("Pic").value.toLowerCase().replace(pic, "");
            o.fixPicString();
            o.reSetUpload(num);
            document.getElementById("GGS_UploadMsg_" + num).innerHTML = "图片删除成功";
            var urllink = "/delpic";
            $.post(urllink, { num: num, picurl: escape(pic) }, function(data, textStatus) { }, "html");
        }
    }
    o.reSetUpload = function(num) {
        o.Cleartimer(num);
        document.getElementById("GGS_UploadImg_" + num).src = "/img/nopic2.gif";
        document.getElementById("GGS_UploadMsg_" + num).innerHTML = "";
        document.getElementById("GGS_UploadInput_" + num).style.display = "";
        document.getElementById("GGS_UploadInput_" + num).disabled = false;
        o.clearvalue(document.getElementById("GGS_UploadInput_" + num));
        document.getElementsByName("GGS_PicCover")[num].checked = false;

        document.getElementById("PicList_" + num).value = "";
        document.getElementById("GGS_Upload_A" + num).style.display = "none";
        document.getElementById("GGS_UploadProgress_" + num).innerHTML = "";
        document.getElementById("GGS_spanCover_" + num).style.display = "none";
        document.getElementById("GGS_UploadCancel_" + num).style.display = "none";
    }

    o.CancelUpload = function(num) {//取消上传图片,仅仅是不保存上传结果, 并不阻止上传.
        o.uplodEnableStr = o.uplodEnableStr.replace(num, "");
        o.reSetUpload(num);

        var datanow = new Date();
        document.getElementById("GGS_UploadTimeID_" + num).value = "t" + datenow.getMinutes() + datenow.getSeconds() + datenow.getMilliseconds();
        var urllink = "/upload";
        var post = "&";
        post += "num=" + num + "&";
        post += "action=cancel";
        $.post(urllink, {}, function(data, textStatus) { }, "html");
    }

    o.Cleartimer = function(num) {
        var objT = eval("o.timerID" + num);
        clearInterval(objT);
    }

    o.AddPic = function() {
        var divobj = document.getElementById(o.divName);
        var piccount = o.ClientPicList.split('|').length;
        if (piccount > 4)
            piccount = 4;
        var style = o.style2.T;
        var timeid = "t" + datenow.getMinutes() + datenow.getSeconds() + datenow.getMilliseconds();
        style = style.replace(/{TimeID}/g, timeid);
        style = style.replace(/{Num}/g, piccount);
        style = style.replace("{onchange}", o.Objname + ".Input_OnChange(this, " + piccount + ")");
        style = style.replace("{imgpath}", "/img/nopic2.gif");
        o.ClientPicList += "|/img/nopic2.gif";
        divobj.innerHTML += style;
    }

    o.style = {
        T: '<div class="upP">' +
			'<form id="GGS_UploadForm_{Num}" name="GGS_UploadForm_{Num}" method="post" enctype="multipart/form-data">' +
			'<span class="uploadPic"><img id="GGS_UploadImg_{Num}" src="{imgpath}" height="64" /></span>' +
			'<p><input name="GGS_UploadInput_{Num}" onchange="{onchange}" id="GGS_UploadInput_{Num}" type="file" class="fen pInput" size="12" /><input type="hidden" id="PicList_{Num}" /></p>' +
			'<p class="fRed"><span id="GGS_UploadMsg_{Num}" class="fOra"></span></p>' +
			'<p><a id="GGS_Upload_A{Num}" style="display:none" href="javascript://" onclick="' + o.Objname + '.DelPic({Num})">删除</a></p>' +
			'<div class="c"></div><input type="hidden" value="' + o.Objname + '.PicUploadFinish" name="backFunction" /><input type="hidden" value="{Num}" name="backNum" /></form>' +
			'</div><iframe style="display:none" src="about:blank" id="formSubmit_{Num}" name="formSubmit_{Num}"></iframe>'
    }
    o.style1 = {
        T: '<li>' +
			'<form id="GGS_UploadForm_{Num}" name="GGS_UploadForm_{Num}" method="post" enctype="multipart/form-data">' +
			'<img id="GGS_UploadImg_{Num}" src="{imgpath}" height="106" width="106" />' +
			'<br /><input name="GGS_UploadInput_{Num}" onchange="{onchange}" id="GGS_UploadInput_{Num}" type="file" class="add" size="4" /><input type="hidden" id="PicList_{Num}" />' +
			'<br /><span id="GGS_UploadMsg_{Num}" class="fOra"></span>' +
			'<p><a id="GGS_Upload_A{Num}" style="display:none"  href="javascript://" onclick="' + o.Objname + '.DelPic({Num})">删除</a></p>' +
			'<input type="hidden" value="' + o.Objname + '.PicUploadFinish" name="backFunction" /><input type="hidden" value="{Num}" name="backNum" /></form>' +
			'<iframe style="display:none" src="about:blank" id="formSubmit_{Num}" name="formSubmit_{Num}"></iframe></li>'
    }
    o.style2 = {
        T: '<li>' +
			'<img id="GGS_UploadImg_{Num}" src="{imgpath}" height="106" width="106" />' +
			'<input name="GGS_UploadInput_{Num}" onchange="{onchange}" id="GGS_UploadInput_{Num}" type="file" class="add" size="4" /><input type="hidden" id="PicList_{Num}" />' +
			'<br /><span id="GGS_UploadMsg_{Num}" class="fOra"></span>' +
			'<br /><span id="GGS_UploadProgress_{Num}"></span>' +
			'<input type="hidden" value="{TimeID}" name="GGS_UploadTimeID_{Num}" id="GGS_UploadTimeID_{Num}" />' +
			'<span style="clear:both;" ></span><a id="GGS_UploadCancel_{Num}" style="vertical-align:bottom;display:none; float:left;"  href="javascript://" onclick="' + o.Objname + '.CancelUpload({Num})">取消</a>' +
			'<label id="GGS_spanCover_{Num}" style="display:none;"><input type="radio" name="GGS_PicCover" onclick="' + o.Objname + '.SetCover({Num})" />封面</label>&nbsp;&nbsp;&nbsp;' +
			'<a id="GGS_Upload_A{Num}" style="display:none;"  href="javascript://" onclick="' + o.Objname + '.DelPic({Num})">删除</a>' +
			'<iframe style="display:none" src="about:blank" id="formSubmit_{Num}" name="formSubmit_{Num}"></iframe>'
    }
    o.Hid = {
        h: '<input type="hidden" value="' + o.Objname + '.PicUploadFinish" name="backFunction" id="backFunction"/><input type="hidden"  name="backNum" id="backNum"/>'
    }
    o.clearvalue = function(file) {
        var form = document.createElement('form');
        document.body.appendChild(form);
        //记住file在旧表单中的的位置
        var pos = file.nextSibling;
        form.appendChild(file);
        form.reset();
        pos.parentNode.insertBefore(file, pos);
        document.body.removeChild(form);
    }
    //dom.loadCss("http://pix.58.com/css/4_0/jscss/CheckFormCSS");
}